<?php
    
    /**
     * 
     * 
     * 
     * 
     * 
     */
    
	class mainController {
		
        var $_p;
         
        /*
         * 
         */ 
        public function __construct(){
            include_once('source/assets/ClassPlaylist.php');
            $this->_p = new Playlist();
        }    
            
		/*
         * 
         */
		public function indexAction(){
		
            $shows      = $this->_p->getShowJson();
            $schedule   = $this->_p->getScheduleJson(true);
            
            $sw            = $this->_p->getScheduleJson();
            $week_schedule = $this->_p->buildTimelineWeek($sw);
            
            include_once('source/assets/ClassPodcast.php');
            $_pc = new Podcast();
            
            $podcasts = $_pc->feeds;
            
            
            return array(
                'shows'             => $shows,
                'schedule'          => $schedule,
                'week_schedule'     => $week_schedule,
                'podcasts'          => $podcasts
            );
			
		}
        
        /*
         * 
         */
        public function adminAction(){
            return $this->indexAction();
        }
        
        /*
         * 
         */ 
        public function nextsongAction(){
            $this->_p->getRemoteUrl(TRANSCODER_BASE.'nextsong','','');
        }
        
        /*
         * 
         */ 
        public function loadplaylistAction(){
            $slug = getParams('slug');
            $show = $this->_p->getShowJson($slug);
            
            if(!$show)
                return;

            $this->_p->generate($slug);
            $this->_p->setNowPlaying($show['slug']);
            $this->_p->reloadPlaylist($show['name']);
        }

        /*
         * 
         */ 
        public function shoutcastAction(){
            $url = SC_SERVER_BASE.'currentsong';
            
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
        
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); 
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_ENCODING, "gzip");
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $data = curl_exec($curl);
            curl_close($curl);
            
            $show = $this->_p->getNowplaying();
            unset($show['folder'],$show['hot_rotation']);
            
            include_once('source/assets/ClassPodcast.php');
            $_pc = new Podcast();
            
            $podcast = $_pc->feeds[$show['podcast']['slug']];
            
            echo json_encode( array( 
                'song'      => $data,
                'show'      => $show,
                'podcast'   => $podcast
                
                )
            );
        }
        
        /*
         *
         */ 
        public function songhistoryAction(){
            include_once('source/assets/ClassShoutcast.php');
            $_sc         = new shoutcast(SC_SERVER_IP,SC_SERVER_PORT,SC_SERVER_PASS);
            $songhistory = $_sc->getShoutcastHistory();
            return array('songhistory'=>$songhistory);
        }
        
        /*
         * 
         */
        public function listenerAction(){
            include_once('source/assets/ClassShoutcast.php');
            $_sc = new shoutcast(SC_SERVER_IP,SC_SERVER_PORT,SC_SERVER_PASS);
            $data = $_sc->getShoutcastData();
            return array('listener'=>$data['LISTENERS']);
        }
        
        /**
         * 
         */
        public function schedulecronjobAction(){
            $this->_p->scheduleCronjob();
        }
        
        public function schedulepodcastAction(){
            include_once('source/assets/ClassPodcast.php');
            $pc = new Podcast();
            
            $pc->schedulePodcast();
        }

	}
?>